package com.server.common.utils.pdf;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

/**
 * Created by Administrator on 2016/12/1.
 */
public class ExportPdf {
    private Document document;
    private String path;//文件路径
    public ExportPdf(String path){
        this.path= path;
        document  = new Document();
        try {
            PdfWriter.getInstance(document, new FileOutputStream(this.path));
            document.open();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    private void Analyzer(PDFItemModel pdfItemModel){
        switch (pdfItemModel.getType()){
            case "image":
                Image img=null;
                if (pdfItemModel.getData().getClass().getName().equals("java.util.ArrayList")){
                    List<String> imgs =(List<String>)pdfItemModel.getData();
                    for (String filePath :imgs){
                        img = getImage(filePath);
                    }
                }else{
                    String filePath = (String)pdfItemModel.getData();
                    img = getImage(filePath);
                }
                break;
            case "text":
                break;
            default:
                break;
        }

        //添加新的节点


    }
    protected void finalize( )
    {
        if (document!=null){
            document.close();
            document=null;
        }
    }

    private Image getImage(String path){
        Image image = null;
        try {
            image  = Image.getInstance(path);
        }catch (BadElementException e){
                e.printStackTrace();
        }catch ( MalformedURLException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
                return image;
        }
    }

}
